package com.example.designpatten.chapter16_statepattern;

public class StatePattern {
    public static void main(String[] args) {
        //紧急项目
        Work emergencyProjects = new Work();
        emergencyProjects.setHour(9);
        emergencyProjects.WriteProgram();

        emergencyProjects.setHour(10);
        emergencyProjects.WriteProgram();

        emergencyProjects.setTaskFinsh(false);
        emergencyProjects.setHour(20);
        emergencyProjects.WriteProgram();
    }
}

abstract class State {
    public abstract void WriteProgram(Work work);
}

//上午工作状态
class ForenoonState extends State {
    @Override
    public void WriteProgram(Work w) {
        if (w.getHour() < 12) {
            System.out.println("当前时间：" + w.getHour() + "点 上午工作，精神百倍");
        } else {
            w.setState(new NoonState());
            w.WriteProgram();
        }
    }
}

//中午工作状态
class NoonState extends State {
    @Override
    public void WriteProgram(Work w) {
        if (w.getHour() < 13) {
            System.out.println("当前时间：" + w.getHour() + "点 饿了，午饭；犯困，午休。");
        } else {
            w.setState(new AfternoonState());
            w.WriteProgram();
        }
    }
}

//下午工作状态
class AfternoonState extends State {
    @Override
    public void WriteProgram(Work w) {
        if (w.getHour() < 17) {
            System.out.println("当前时间：" + w.getHour() + "点 下午状态还不错，继续努力");
        } else {
            w.setState(new EveningState());
            w.WriteProgram();
        }
    }
}

//晚间工作状态
class EveningState extends State {
    @Override
    public void WriteProgram(Work w) {
        if (w.getTaskFinsh()) {
            w.setState(new RestState());
            w.WriteProgram();
        } else {
            if (w.getHour() < 21) {
                System.out.println("当前时间：" + w.getHour() + "点 加班哦，疲累之极");
            } else {
                w.setState(new SleepingState());
                w.WriteProgram();
            }
        }
    }
}

//睡眠状态
class SleepingState extends State {
    @Override
    public void WriteProgram(Work w) {
        System.out.println("当前时间：" + w.getHour() + "点 不行了，睡着了。");
    }
}

//下班休息状态
class RestState extends State {
    @Override
    public void WriteProgram(Work w) {
        System.out.println("当前时间：" + w.getHour() + "点 下班回家了");
    }
}

class Work {
    private State state;

    public Work() {
        this.state = new ForenoonState();
    }

    private double hour;

    private Boolean taskFinsh;

    public void WriteProgram() {
        state.WriteProgram(this);
    }

    public State getState() {
        return state;
    }

    public void setState(State state) {
        this.state = state;
    }

    public double getHour() {
        return hour;
    }

    public void setHour(double hour) {
        this.hour = hour;
    }

    public Boolean getTaskFinsh() {
        return taskFinsh;
    }

    public void setTaskFinsh(Boolean taskFinsh) {
        this.taskFinsh = taskFinsh;
    }
}